#include "List.h"


ListNode* LTInit() {
	LTNode* phead = buy(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;

}
void LTprint(LTNode* phead) {
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead) {
		printf("%d<=>", cur->val);
		cur = cur->next;
	}
	printf("\n");
}

LTNode* buy(int x) {
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->val = x;
	return newnode;
}

void LTPushback(LTNode* phead, int x) {
	assert(phead);

	LTNode* newnode = buy(x);
	LTNode* tail = phead->prev;

	//
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;

}
bool LTempty(LTNode* phead) {
	assert(phead);

	if (phead->next == phead)
		return true;
	else
		return false;
}

void LTPopback(LTNode* phead) {
	assert(phead);
	assert(!LTempty(phead));
	LTNode* tail = phead->prev;
	LTNode* tailprev = tail->prev;

	tailprev->next = phead;
	phead->prev = tailprev;
	free(tail);
	tail = NULL;
}

void LTinsert(LTNode* pos, int x) {
	assert(pos);

	LTNode* n = buy(x);
	LTNode* pre = pos->prev;

	pos->prev = n;
	n->next = pos;
	pre->next = n;
	n->prev = pre;

}

void LTpushfront(LTNode* phead,int x) {
	assert(phead);
	LTinsert(phead->next,x);
}

void LTerase(LTNode* pos) {
	assert(pos);
	LTNode* p = pos->prev;
	LTNode* n = pos->next;
	
	p->next = n;
	n->prev = p;
	
	free(pos);
	pos = NULL;
}

void LTpopfront(LTNode* phead) {
	LTerase(phead->next);
};